Conditions | 14 |
Paths | 1057 |
Total Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like index.js ➔ encrypt often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* global hexo, __dirname */ |
||
9 | hexo.extend.filter.register('after_post_render', function encrypt (data) { |
||
10 | |||
11 | // Close the encrypt function |
||
12 | if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) { |
||
13 | |||
14 | return data; |
||
15 | |||
16 | } |
||
17 | if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template |
||
18 | |||
19 | hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html')); |
||
20 | |||
21 | } |
||
22 | if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info |
||
23 | |||
24 | hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>'; |
||
25 | |||
26 | } |
||
27 | if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message |
||
28 | |||
29 | hexo.config.encrypt.default_message = 'Please enter the password to read the blog.'; |
||
30 | |||
31 | } |
||
32 | if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // wrong password |
||
33 | |||
34 | hexo.config.encrypt.default_decryption_error = 'Incorrect Password!'; |
||
35 | |||
36 | } |
||
37 | if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // no content |
||
38 | |||
39 | hexo.config.encrypt.default_no_content_error = 'No content to display!'; |
||
40 | |||
41 | } |
||
42 | |||
43 | if ('password' in data && data.password) { |
||
44 | |||
45 | // Use the blog's config first |
||
46 | console.log(`encrypt the blog :${ data.title.trim() }`); |
||
|
|||
47 | |||
48 | // Store the origin data |
||
49 | data.origin = data.content; |
||
50 | data.encrypt = true; |
||
51 | |||
52 | if (!('abstract' in data && data.abstract)) { |
||
53 | |||
54 | data.abstract = hexo.config.encrypt.default_abstract; |
||
55 | |||
56 | } |
||
57 | if (!('template' in data && data.template)) { |
||
58 | |||
59 | data.template = hexo.config.encrypt.default_template; |
||
60 | |||
61 | } |
||
62 | if (!('message' in data && data.message)) { |
||
63 | |||
64 | data.message = hexo.config.encrypt.default_message; |
||
65 | |||
66 | } |
||
67 | if (!('decryptionError' in data && data.decryptionError)) { |
||
68 | |||
69 | data.decryptionError = hexo.config.encrypt.default_decryption_error; |
||
70 | |||
71 | } |
||
72 | if (!('noContentError' in data && data.noContentError)) { |
||
73 | |||
74 | data.noContentError = hexo.config.encrypt.default_no_content_error; |
||
75 | |||
76 | } |
||
77 | |||
78 | data.content = escape(data.content); |
||
79 | data.content = CryptoJS.enc.Utf8.parse(data.content); |
||
80 | data.content = CryptoJS.enc.Base64.stringify(data.content); |
||
81 | data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString(); |
||
82 | // Console.log(data.content); |
||
83 | data.template = data.template.replace('{{content}}', data.content); |
||
84 | data.template = data.template.replace('{{message}}', data.message); |
||
85 | data.template = data.template.replace('{{message}}', data.message); |
||
86 | data.template = data.template.replace('{{decryptionError}}', data.decryptionError); |
||
87 | data.template = data.template.replace('{{noContentError}}', data.noContentError); |
||
88 | |||
89 | data.content = data.template; |
||
90 | data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script> |
||
91 | <script src="${hexo.config.root}lib/blog-encrypt.js"></script>' |
||
92 | <link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`; |
||
93 | |||
94 | data.more = data.abstract; |
||
95 | data.excerpt = data.more; |
||
96 | |||
97 | } |
||
98 | return data; |
||
99 | |||
100 | }); |
||
101 | |||
114 |